package com.bbzn.device.client.service.emq.data;

import com.alibaba.fastjson.JSONObject;
import com.bbzn.device.client.config.EmqCode;
import com.bbzn.device.client.service.*;
import com.bbzn.device.client.utils.OptionalBean;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @Author wulongbo
 * @Date 2021/1/19 19:48
 * @Version 1.0
 */
@Component
public class EmqDeviceVideoStrategy implements EmqStrategy {

    @Resource
    private PublishService publishService;

    @Resource
    private DeviceService deviceService;

    // 发起视频通话4048 (设备触发) 通知前端
    @Override
    public String emqAction(String payLoad) {
        try {
            JSONObject jsonObject = JSONObject.parseObject(payLoad);
            String imei = jsonObject.getString("imei");
            if (!OptionalBean.ofNullable(imei).isPresent()) {
                return "json解析错误";
            }
            String topic = deviceService.getTopicByImei(imei);
            if(StringUtils.isNotEmpty(topic)){
                publishService.sendToMqtt(payLoad, topic);
            }
        } catch (Exception e) {
            return "发起视频通话失败！";
        }
        return "发起视频通话上报成功！";
    }
}
